package com.sunose.agfmk.examples;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.ViewGroup.LayoutParams;

import com.sunose.agfmk.constant.Constant;
import com.sunose.agfmk.utils.fileUtils;
import com.sunose.agfmk.R;
import android.graphics.drawable.BitmapDrawable;

public class XorActivity extends Activity {
    public final static String tag= Constant.TAG_PREFIX+"_XorActivity";
    int preX=0;
    int preY=0;
     int x=0;
    int y=0;
    int maxX=0;
    int maxY=0;
    ImageView compositeImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xor);
        displayXorImage();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.xor, menu);
        return true;
    }
    public void onButton_quit(View view)
    {
       this.finish();
    }
    //display two image by xor mode.
    public void displayXorImage()
    {
    Log.d(tag, "call displayXorImage");
        fileUtils fu=fileUtils.getInstance(this);

        LinearLayout xor_layout_imagview = (LinearLayout) findViewById(R.id.xor_layout_imagview);
  try
  {
    Bitmap background=fu.readFile("cool-landscape.jpg", "xor");
    Bitmap resultingImage=Bitmap.createBitmap(background.getWidth(), background.getHeight(),background.getConfig());
      Log.d(tag,"width:"+background.getWidth()+" height:"+background.getHeight());

    Canvas canvas = new Canvas(resultingImage);
    Paint paint = new Paint();
      canvas.drawBitmap(background, 0, 0, paint);
      compositeImageView=new ImageView(this); ;
      Log.d(tag,"xor ready to display...");
//      compositeImageView.setDrawingCacheEnabled(true);
      compositeImageView.setImageBitmap(resultingImage);
      compositeImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
      compositeImageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
      Log.d(tag,"layout width:"+xor_layout_imagview.getWidth()
      +",height:"+xor_layout_imagview.getHeight()+",CENTER_CROP...setlay");
      xor_layout_imagview.addView(compositeImageView);

  }catch(Exception e)
  {
   e.printStackTrace();
  }
}
    //change bug photo coordationa
    public void xorMove(View view) throws Exception
    {
      preX=x;
      preY=y;
      x=x+10;
      y=y+10;
        drawBug(x,y);
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        //get/set the size here.
        LinearLayout xor_layout_imagview = (LinearLayout) findViewById(R.id.xor_layout_imagview);
        Log.d(tag,"onWindowFocusChanged layout width:"+xor_layout_imagview.getWidth()
                +",height:"+xor_layout_imagview.getHeight()+",CENTER_CROP...onWindowFocusChanged");
        maxX=xor_layout_imagview.getWidth();
        maxY=xor_layout_imagview.getHeight();
    }
    public void drawBug(int x,int y) throws Exception
    {
        fileUtils fu=fileUtils.getInstance(this);

        Bitmap bitmap1= fu.readFile("firebug.png","xor");

        //mask
        Bitmap mask=Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas mask_c=new Canvas(mask);
        Bitmap old_bitmap=((BitmapDrawable)compositeImageView.getDrawable()).getBitmap();
        Canvas canvas=new Canvas(old_bitmap);

        mask_c.drawRGB(0xff,0xff,0xff);//white background
        Paint paint = new Paint();
//      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
        //add bug
        canvas.drawBitmap(bitmap1, x, y, paint);//draw bug
        compositeImageView.setImageBitmap(old_bitmap);
    }
}
